package com.henanuniversity.utils;

import java.sql.*;

public class JdbcUtils {
    //static String url="jdbc:mysql://localhost:3308/users?useUnicode=true&&characterEncoding=UTF-8&&serverTimezone=UTC";
    static String url="jdbc:mysql://localhost:3306/elm?useUnicode=true&&characterEncoding=UTF-8&&serverTimezone=UTC";
    static String username="root";
    static String password="123456";
    static String driverClassName="com.mysql.cj.jdbc.Driver";
    static {
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection connection=null;
        try {
             connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){

        //关闭结果集
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //关闭Statement
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //关闭数据库连接
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void beginTransaction(){

    }

    public static void commitTransaction(){

    }
    public static void rollbackTransaction(){

    }

}
